package cc.android.testapp.ui.part;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import cc.android.testapp.R;

public class EnableButton extends android.support.v7.widget.AppCompatButton {

    public boolean enable = false;
    public String prefix = null;
    public String suffix = null;

    public EnableButton(Context context) {
        super(context);
    }

    public EnableButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray tTr = context.obtainStyledAttributes(attrs, R.styleable.EnableButton);
        this.enable = tTr.getBoolean(R.styleable.EnableButton_def_stat, false);
        this.prefix = tTr.getString(R.styleable.EnableButton_prefix);
        this.suffix = tTr.getString(R.styleable.EnableButton_suffix);
        tTr.recycle();
        this.updateText();
    }


    public void switchStat() {
        this.enable = !this.enable;
        this.updateText();
    }

    public void updateText() {
        StringBuilder tSBuilder = new StringBuilder();
        if (prefix != null) tSBuilder.append(prefix);
        tSBuilder.append(enable ? "关闭" : "启用");
        if (suffix != null) tSBuilder.append(suffix);
        this.setText(tSBuilder.toString());
    }
}
